package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.*;
import com.cpj.bsyyzx.mapper.BedMapper;
import com.cpj.bsyyzx.mapper.FileNumMapper;
import com.cpj.bsyyzx.mapper.NurseClassMapper;
import com.cpj.bsyyzx.mapper.StaffManagementMapper;
import com.cpj.bsyyzx.service.*;
import com.cpj.bsyyzx.util.CommonResult;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/user")
@Log4j
public class UserController {
    @Resource
    IUserService userService;
    @Resource
    FileNumMapper fileNumMapper;
    @Resource
    IFileNumService fileNumService;
    @Resource
    BedMapper bedMapper;
    @Resource
    StaffManagementMapper staffManagementMapper;
    @Autowired
    BedRecordController bedRecordController;
    @Autowired
    IBedRecordService bedRecordService;
    @Autowired
    IBedUserService bedUserService;
    @Autowired
    BedUserController bedUserController;
    @Resource
    NurseClassMapper nurseClassMapper;

    @RequestMapping("/pagelist")
    CommonResult pagelist(Page page) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        IPage<User> rpage = userService.selectPageVo(page, queryWrapper);
        return CommonResult.success(rpage);
    }

    @RequestMapping("/list")
    CommonResult list(Page page, User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        Page<User> rpage = userService.pageList(page, user);
        return CommonResult.success(rpage);
    }

    @RequestMapping("save")
    @ResponseBody
    CommonResult save(User user) {
        log.debug("保存");
//      保存床位记录
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("record_num", user.getFileNum());
        BedRecord bedRecord = bedRecordService.getOne(wrapper);
        if (bedRecord == null || "".equals(bedRecord.getRecordNum())) {
            bedRecord = new BedRecord();
            bedRecord.setRecordNum(user.getFileNum());
            bedRecord.setUserName(user.getUserName());
            bedRecord.setSex(user.getSex());
            bedRecord.setBedInformation(user.getBedNumber());
            bedRecord.setStartDate(user.getStartTime());
            bedRecord.setEddingDate(user.getEndTime());
            bedRecord.setState("正常");
            bedRecordController.saveDoing(bedRecord);
        } else {
            bedRecord.setUserName(user.getUserName());
            bedRecord.setSex(user.getSex());
            bedRecord.setBedInformation(user.getBedNumber());
            bedRecord.setStartDate(user.getStartTime());
            bedRecord.setEddingDate(user.getEndTime());
            bedRecord.setState("正常");
            bedRecordController.updateDoing(bedRecord);
        }


        //详细信息
        QueryWrapper bedUserWrapper = new QueryWrapper();
        bedUserWrapper.eq("record_num", user.getFileNum());
        BedUser bedUser = bedUserService.getOne(bedUserWrapper);
        if (bedUser == null) {
            bedUser = new BedUser();
            bedUser.setRecordNum(user.getFileNum());
        }
        bedUser.setUserName(user.getUserName());
        bedUser.setSex(user.getSex());
        bedUser.setNurseClass(user.getCareGrade());
        bedUser.setNurse(user.getNurse());
        bedUser.setNursingWorkers(user.getCarer());
        bedUser.setDoctor(user.getDoctor());
        bedUser.setEmphasis(user.getNote());
        bedUserController.saveOrUpdate(bedUser);


        //bedUser表保存和新建立
        boolean success = userService.saveOrUpdate(user);
        return CommonResult.success(success);
    }

    @RequestMapping("/add")
    @ResponseBody
    CommonResult add(User user) {

        FileNum fileNum = fileNumMapper.selectFileByUser();
        Integer list = fileNum.getMaxnum();
        list += 1;
        fileNum.setMaxnum(list);
        fileNumService.saveOrUpdate(fileNum);
        user.setFileNum("User" + list);


        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("record_num", user.getFileNum());
        BedRecord bedRecord = bedRecordService.getOne(wrapper);
        if (bedRecord == null || "".equals(bedRecord.getRecordNum())) {
            bedRecord = new BedRecord();
            bedRecord.setRecordNum(user.getFileNum());
            bedRecord.setUserName(user.getUserName());
            bedRecord.setSex(user.getSex());
            bedRecord.setBedInformation(user.getBedNumber());
            bedRecord.setStartDate(user.getStartTime());
            bedRecord.setEddingDate(user.getEndTime());
            bedRecord.setState("正常");
            bedRecordController.saveDoing(bedRecord);
        } else {
            bedRecord.setUserName(user.getUserName());
            bedRecord.setSex(user.getSex());
            bedRecord.setBedInformation(user.getBedNumber());
            bedRecord.setStartDate(user.getStartTime());
            bedRecord.setEddingDate(user.getEndTime());
            bedRecord.setState("正常");
            bedRecordController.updateDoing(bedRecord);
        }
        //详细信息
        QueryWrapper bedUserWrapper = new QueryWrapper();
        bedUserWrapper.eq("record_num", user.getFileNum());
        BedUser bedUser = bedUserService.getOne(bedUserWrapper);
        if (bedUser == null) {
            bedUser = new BedUser();
            bedUser.setRecordNum(user.getFileNum());
        }
        bedUser.setUserName(user.getUserName());
        bedUser.setSex(user.getSex());
        bedUser.setNurseClass(user.getCareGrade());
        bedUser.setNurse(user.getNurse());
        bedUser.setNursingWorkers(user.getCarer());
        bedUser.setDoctor(user.getDoctor());
        bedUser.setEmphasis(user.getNote());
        bedUserController.saveOrUpdate(bedUser);

        return CommonResult.success(userService.save(user));

    }

    @RequestMapping("toedit")
    @ResponseBody
    CommonResult toedit(ModelMap modelMap, Integer id) {
        log.debug("跳转到修改页面");
//        User user = userService.getById(id);
//        modelMap.addAttribute("user", user);
        return CommonResult.success(userService.getById(id));
    }

    @RequestMapping("del/{id}")
    @ResponseBody
    CommonResult delete(@PathVariable Integer id) {
        log.debug("根据主键删除:" + id);
        boolean success = deleteDoing(id);
        return CommonResult.success(success);
    }

    /**
     * 不保留床位
     * 删除床位管理图
     * 删除床位管理详细数据
     * 改变状态为退住
     *
     * @param id
     * @return
     */
    boolean deleteDoing(Integer id) {
        User user = userService.getById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bed_information", user.getBedNumber());
        BedRecord bedRecord = bedRecordService.getOne(wrapper);
        //删除床位管理表数据
        bedRecordController.deleteDoing(bedRecord.getBedId());
        //删除床位详细信息
        bedUserController.delete(user.getFileNum());

        user.setState("已退住");
        user.setBedNumber(null);
        return userService.saveOrUpdate(user);
    }

    /**
     * 保存床位的删除
     * 将床位管理图的状态改成退住
     * 改变user表的床位状态和普通状态
     *
     * @param id
     * @return
     */
    boolean deleteByRetainBed(Integer id) {
        User user = userService.getById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bed_information", user.getBedNumber());
        BedRecord bedRecord = bedRecordService.getOne(wrapper);
        bedRecord.setState("退住");
        bedRecordController.updateDoing(bedRecord);
        user.setState("保留退住");
        return userService.saveOrUpdate(user);
    }

    /**
     * 将客户信息变更为离开
     * @param id
     * @return
     */
    boolean updateToLeft(Integer id){
        User user=userService.getById(id);
        user.setState("离开");
        return userService.saveOrUpdate(user);
    }

    /**
     * 将离开的用户变为正常
     * @param id
     * @return
     */
    boolean comeBack(Integer id){
        User user=userService.getById(id);
        user.setState("正常");
        return userService.saveOrUpdate(user);
    }

    @RequestMapping("recovery")
    @ResponseBody
    CommonResult recovery(User user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bed_information", user.getBedNumber());
        BedRecord bedRecord = bedRecordService.getOne(wrapper);
        bedRecord.setState("正常");
        bedRecordController.updateDoing(bedRecord);
        user.setState("正常");
        return CommonResult.success(userService.saveOrUpdate(user));
    }

    @RequestMapping("findUser")
    @ResponseBody
    CommonResult findUser() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("state", "正常");
        return CommonResult.success(userService.list(wrapper));
    }

    @RequestMapping("bed")
    @ResponseBody
    CommonResult selectBedNum() {
        log.debug("查找没人的床位:");

        QueryWrapper wrapper = new QueryWrapper();

        return CommonResult.success(bedMapper.selectBedNum(wrapper));
    }

    @RequestMapping("doctor")
    @ResponseBody
    CommonResult selectdoctor() {
        log.debug("查找没人的床位:");

        QueryWrapper wrapper = new QueryWrapper();

        return CommonResult.success(staffManagementMapper.selectDoctor());
    }

    @RequestMapping("carer")
    @ResponseBody
    CommonResult selectcarer() {
        log.debug("查找没人的床位:");

        QueryWrapper wrapper = new QueryWrapper();

        return CommonResult.success(staffManagementMapper.selectCarer());
    }

    @RequestMapping("nurse")
    @ResponseBody
    CommonResult selectnurse() {
        log.debug("查找没人的床位:");

        QueryWrapper wrapper = new QueryWrapper();

        return CommonResult.success(staffManagementMapper.selectNurse());
    }

    @RequestMapping("steward")
    @ResponseBody
    CommonResult selectsteward() {
        log.debug("查找没人的床位:");

        QueryWrapper wrapper = new QueryWrapper();

        return CommonResult.success(staffManagementMapper.selectSteward());
    }

    @RequestMapping("nclass")
    @ResponseBody
    CommonResult selectnurseclass() {
        log.debug("查找护理等级:");
        QueryWrapper wrapper = new QueryWrapper();
        return CommonResult.success(nurseClassMapper.selectNclass());
    }

    @RequestMapping("userlist")
    @ResponseBody
    CommonResult userList() {
        return CommonResult.success(userService.list());
    }

}
